package com.vooda.controller;

import com.vooda.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author charlee
 * @date 2017-11-30
 */
public class BaseController {

    @Autowired
    HttpServletRequest request;

    /**
     * 获取请求头的token
     *
     * @return token
     */
    String getToken() {
        String token = request.getHeader("token");
        return StringUtils.hasText(token) ? token : "";
    }

    /**
     * 获取用户id
     *
     * @return userId
     */
    Long getUserId() {
        String token = request.getHeader("token");
        Long userId = JwtUtil.validateToken(token);
        if (userId == null) {
            return 0L;
        }
        return userId;
    }
}
